from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
import os

# import pymysql


class Application(Flask):
    def __init__(self, import_name, template_folder=None, root_path=None):
        super(Application, self).__init__(import_name, template_folder=template_folder,
                                          root_path=root_path,
                                          static_folder=None)
        self.config.from_pyfile("config/base_setting.py")
        if "ops_config" in os.environ:
            self.config.from_pyfile("config/%s_setting.py" % os.environ['ops_config'])
        # 在linux系统下，输入"export ops_config=local"时，自动加载local_setting配置文件
        # 在Windows系统下，输入"set ops_config=local"时，自动加载local_setting配置文件

        db.init_app(self)


# db = pymysql.connect("localhost", "root", "123456", "mysql", charset="utf8")
# cur = db.cursor()
db = SQLAlchemy()
app = Application(__name__, template_folder=os.getcwd()+"/web/templates/", root_path=os.getcwd())
manager = Manager(app)


from common.libs.UrlManager import UrlManager   # 注意导入顺序

# 函数模板
app.add_template_global(UrlManager.buildStaticUrl, "buildStaticUrl")
app.add_template_global(UrlManager.buildUrl, "buildUrl")
app.add_template_global(UrlManager.buildImageUrl, "buildImageUrl")
